New features of AbiPy v0.7¶

M. Giantomassi and the AbiPy group¶

9th international ABINIT developer workshop
20-22nd May 2019 - Louvain-la-Neuve, Belgium


  • These slides have been generated using jupyter, nbconvert and revealjs
  • The notebook can be downloaded from this github repo
  • To install and configure the software, follow these installation instructions

Use the Space key to navigate through all slides.

What is AbiPy?¶

Python package for:¶

  • Generating ABINIT input files automatically
  • Post-processing output results (netcdf and text files)
  • Interfacing ABINIT with external tools (e.g. Vesta)
  • Creating and executing workflows (band structures, DFPT, $GW$, BSE…)

Why python?¶

  • Easy to use and to learn
  • Great support for science (numpy, scipy, pandas, matplotlib …)
  • Interactive environments (ipython, jupyter notebooks, GUIs)
  • More powerful and flexible than Fortran for implementing the high-level logic needed in modern ab-initio workflows
  • pymatgen ecosystem and the materials project database…

How to install AbiPy¶

Using pip and python wheels:

    pip install abipy --user

Using conda (recommended):

    conda install abipy --channel abinit 

From the github repository (develop mode):

    git clone https://github.com/abinit/abipy.git
    cd abipy 
    python setup.py develop


For further info see http://abinit.github.io/abipy/installation.html

AbiPy documentation with galleries of matplotlib examples and workflows¶

In [2]:
%embed https://abinit.github.io/abipy/index.html
Out[2]:

Jupyter notebooks with examples and lessons inspired by the official tutorials¶

In [3]:
%embed https://nbviewer.jupyter.org/github/abinit/abitutorials/blob/master/abitutorials/index.ipynb
Out[3]:

Command line interface¶

  • abiopen.py ➝ Open output files inside ipython or print/visualize file
  • abistruct.py ➝ Operate on crystalline structures read from file
  • abicomp.py ➝ Compare multiple files (i.e. convergence studies)
  • abiview.py ➝ Quick visualization of output files
  • abinp.py ➝ Generate input files for typical calculations

Documentation¶

  • abistruct.py --help for manpage
  • abistruct.py COMMAND --help for help about COMMAND
    HTML documentation available at http://abinit.github.io/abipy/scripts/index.html

Examples¶

abistruct.py spglib si_scf_GSR.nc
abistruct.py convert si_scf_GSR.nc -f cif
abiopen.py si_scf_GSR.nc --print

and many more...¶

In [4]:
gsr = abiopen("si_nscf_GSR.nc")
gsr.ebands.plotly(with_gaps=True);
In [6]:
#gsr.ebands.plotly_with_edos(abidata("si_scf_GSR.nc"));
gsr.ebands.kpoints.plotly();
In [7]:
abilab.abipanel()
Out[7]:
<module 'panel' from '/Users/gmatteo/miniconda3/envs/env3.7/lib/python3.7/site-packages/panel/__init__.py'>
In [8]:
gsr.get_panel()
Out[8]:
In [9]:
gsr.structure.get_panel()
Out[9]:
In [11]:
ddb = abilab.abiopen("ZnSe_hex_qpt_DDB")

ddb.get_panel()
Out[11]:

To produce a predefined set of matplotlib figures, use:¶

abiopen.py mgb2_kpath_FATBANDS.nc --expose --seaborn

abiopen_expose

Replace --expose with --notebook to generate a jupyter notebook with predefined python code¶

abiopen.py supports output files (note abo extension):¶

In [12]:
!abiopen.py gs_dfpt.abo -p 
Traceback (most recent call last):
  File "/Users/gmatteo/miniconda3/envs/env3.8/bin/abiopen.py", line 7, in <module>
    exec(compile(f.read(), __file__, 'exec'))
  File "/Users/gmatteo/git_repos/abipy/abipy/scripts/abiopen.py", line 347, in <module>
    sys.exit(main())
  File "/Users/gmatteo/miniconda3/envs/env3.8/lib/python3.8/site-packages/monty-2021.3.3-py3.8.egg/monty/functools.py", line 405, in wrapper
    sys.exit(main())
  File "/Users/gmatteo/git_repos/abipy/abipy/scripts/abiopen.py", line 213, in main
    raise RuntimeError("%s: no such file" % options.filepath)
RuntimeError: gs_dfpt.abo: no such file

and log files as well:¶

In [13]:
!abiopen.py run.log -p 
Traceback (most recent call last):
  File "/Users/gmatteo/miniconda3/envs/env3.8/bin/abiopen.py", line 7, in <module>
    exec(compile(f.read(), __file__, 'exec'))
  File "/Users/gmatteo/git_repos/abipy/abipy/scripts/abiopen.py", line 347, in <module>
    sys.exit(main())
  File "/Users/gmatteo/miniconda3/envs/env3.8/lib/python3.8/site-packages/monty-2021.3.3-py3.8.egg/monty/functools.py", line 405, in wrapper
    sys.exit(main())
  File "/Users/gmatteo/git_repos/abipy/abipy/scripts/abiopen.py", line 213, in main
    raise RuntimeError("%s: no such file" % options.filepath)
RuntimeError: run.log: no such file

If you are still using perl scripts to extract data from the main output file, check this out:¶

abiopen.py gs_dfpt.abo --expose --seaborn

The introduction of Yaml documents in the main output (cfr. Theo's talk) will facilitate the implementation of new post-processing tools...¶

It works with any file providing a structure object (.nc, .abi, .cif …)¶

Convert structure from netcdf format to CIF (abivars, xsf, poscar, qe, siesta, wannier90, json)¶

In [14]:
!abistruct.py convert si_scf_GSR.nc -f cif
# generated using pymatgen
data_Si
_symmetry_space_group_name_H-M   'P 1'
_cell_length_a   3.86697464
_cell_length_b   3.86697464
_cell_length_c   3.86697464
_cell_angle_alpha   60.00000000
_cell_angle_beta   60.00000000
_cell_angle_gamma   60.00000000
_symmetry_Int_Tables_number   1
_chemical_formula_structural   Si
_chemical_formula_sum   Si2
_cell_volume   40.88829233
_cell_formula_units_Z   2
loop_
 _symmetry_equiv_pos_site_id
 _symmetry_equiv_pos_as_xyz
  1  'x, y, z'
loop_
 _atom_site_type_symbol
 _atom_site_label
 _atom_site_symmetry_multiplicity
 _atom_site_fract_x
 _atom_site_fract_y
 _atom_site_fract_z
 _atom_site_occupancy
  Si  Si0  1  0.00000000  0.00000000  0.00000000  1
  Si  Si1  1  0.25000000  0.25000000  0.25000000  1

Are you still using cut3d to export the structure in a format that other applications can understand?¶

abistruct.py visualize run_si02.abi

Supports also ovito, xcrysden, vtk, mayavi, matplotlib (Use --application option)¶

Need to call anaddb to compute and visualize ph-bands and DOS from DDB?¶

abiview.py ddb ZnSe_hex_qpt_DDB --seaborn

To compare multiple structures:¶

In [16]:
!abicomp.py structure *.cif si_nscf_GSR.nc `find . -name *_DDB`
zsh:1: no matches found: *.cif

Note shell wildcard characters and Unix find inside backticks (bash rocks!)¶

In [17]:
#plotter = abilab.ElectronBandsPlotter()
#plotter.add_ebands(label="BZ sampling", bands="si_scf_GSR.nc")
#plotter.add_ebands(label="k-path", bands="si_nscf_GSR.nc")
#plotter.gridplot(with_gaps=True);

Using ElectronBandsPlotter to visualize multiple band structures:¶

abicomp.py ebands *_GSR.nc

Bash is handy but python is more flexible¶

Let's use the DdbRobot to compare phonons obtained with different ${\bf k}$-meshes and smearing values:¶
In [18]:
paths = [
    "mgb2_888k_0.01tsmear_DDB",
    "mgb2_888k_0.04tsmear_DDB",
    "mgb2_121212k_0.01tsmear_DDB",
    "mgb2_121212k_0.04tsmear_DDB",
]

paths = [os.path.join(abidata.dirpath, "refs", "mgb2_phonons_nkpt_tsmear", f) 
         for f in paths]

robot = abilab.DdbRobot()
for i, path in enumerate(paths):
    robot.add_file(path, path)
In [19]:
# Define function to change labels:
func = lambda ddb: "nkpt: %s, tsmear: %.2f" % (
    ddb.header["nkpt"], ddb.header["tsmear"])

robot.remap_labels(func)
robot
Out[19]:
  1. nkpt: 256, tsmear: 0.01
  2. nkpt: 256, tsmear: 0.04
  3. nkpt: 864, tsmear: 0.01
  4. nkpt: 864, tsmear: 0.04

Now we can build a dataframe with the most important parameters:¶

In [20]:
robot.get_params_dataframe()
Out[20]:
nkpt nsppol ecut tsmear occopt ixc nband usepaw
nkpt: 256, tsmear: 0.01 256 1 35.0 0.01 4 1 8 0
nkpt: 256, tsmear: 0.04 256 1 35.0 0.04 4 1 8 0
nkpt: 864, tsmear: 0.01 864 1 35.0 0.01 4 1 8 0
nkpt: 864, tsmear: 0.04 864 1 35.0 0.04 4 1 8 0

and check that all DDBs have been computed with the same crystalline structure:¶

In [21]:
robot.get_lattice_dataframe()
Out[21]:
formula natom alpha beta gamma a b c volume abispg_num spglib_symb spglib_num spglib_lattice_type
nkpt: 256, tsmear: 0.01 Mg1 B2 3 90.0 90.0 120.0 3.086 3.086 3.523 29.055953 0 P6/mmm 191 hexagonal
nkpt: 256, tsmear: 0.04 Mg1 B2 3 90.0 90.0 120.0 3.086 3.086 3.523 29.055953 0 P6/mmm 191 hexagonal
nkpt: 864, tsmear: 0.01 Mg1 B2 3 90.0 90.0 120.0 3.086 3.086 3.523 29.055953 0 P6/mmm 191 hexagonal
nkpt: 864, tsmear: 0.04 Mg1 B2 3 90.0 90.0 120.0 3.086 3.086 3.523 29.055953 0 P6/mmm 191 hexagonal

To analyze the effect of k-sampling/smearing on the vibrational properties:¶

In [22]:
# Invoke anaddb and store results
r = robot.anaget_phonon_plotters(nqsmall=2)

r.phbands_plotter.gridplot_with_hue("tsmear", with_dos=True);

Input file for band structure calculation + DOS¶

  1. GS run to get the density
  2. NSCF run along high-symmetry k-path
  3. NSCF run with k-mesh to compute the DOS
In [23]:
multi = abilab.ebands_input(structure="si.cif", 
                            pseudos="14si.pspnc",
                            ecut=8, 
                            spin_mode="unpolarized", 
                            smearing=None, 
                            dos_kppa=5000)

multi.get_vars_dataframe("kptopt", "iscf", "ngkpt")
---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
<ipython-input-23-bb8704306b8e> in <module>
      4                             spin_mode="unpolarized",
      5                             smearing=None,
----> 6                             dos_kppa=5000)
      7 
      8 multi.get_vars_dataframe("kptopt", "iscf", "ngkpt")

~/git_repos/abipy/abipy/abio/factories.py in ebands_input(structure, pseudos, kppa, nscf_nband, ndivsm, ecut, pawecutdg, scf_nband, accuracy, spin_mode, smearing, charge, scf_algorithm, dos_kppa)
    235             to be used for the computation of the DOS (None if DOS is not wanted).
    236     """
--> 237     structure = Structure.as_structure(structure)
    238 
    239     if dos_kppa is not None and not isinstance(dos_kppa, (list, tuple)):

~/git_repos/abipy/abipy/core/structure.py in as_structure(cls, obj)
    172 
    173         if is_string(obj):
--> 174             return cls.from_file(obj)
    175 
    176         if isinstance(obj, collections.abc.Mapping):

~/git_repos/abipy/abipy/core/structure.py in from_file(cls, filepath, primitive, sort)
    280             # Invoke pymatgen and change class
    281             # Note that AbinitSpacegroup is missing here.
--> 282             new = super().from_file(filepath, primitive=primitive, sort=sort)
    283             if new.__class__ != cls: new.__class__ = cls
    284 

~/miniconda3/envs/env3.7/lib/python3.7/site-packages/pymatgen-2022.0.5-py3.7-macosx-10.7-x86_64.egg/pymatgen/core/structure.py in from_file(cls, filename, primitive, sort, merge_tol)
   2426 
   2427         fname = os.path.basename(filename)
-> 2428         with zopen(filename, "rt") as f:
   2429             contents = f.read()
   2430         if fnmatch(fname.lower(), "*.cif*") or fnmatch(fname.lower(), "*.mcif*"):

~/miniconda3/envs/env3.7/lib/python3.7/site-packages/monty-2021.3.3-py3.7.egg/monty/io.py in zopen(filename, *args, **kwargs)
     41     if ext in (".GZ", ".Z"):
     42         return gzip.open(filename, *args, **kwargs)
---> 43     return io.open(filename, *args, **kwargs)
     44 
     45 

FileNotFoundError: [Errno 2] No such file or directory: 'si.cif'

To build an input for SCF+NSCF run with (relaxed) structure from the materials project database:¶

    abinp.py ebands mp-149          

Future developments¶

Post-processing tools¶

  • Support for more netcdf files
  • More post-processing tools for MD calculations
  • More converters and interfaces for third-party applications
  • Integrate AbiPy with jupyterlab to create a flexible graphical enviroment for Abinit exposing (part) of the python API
  • Explore web-based technologies for data analysis and visualization (plotly, dash)
  • Develop toolkit to build web apps powered by AbiPy and pymatgen to disseminate scientific results.

Continuous Integration¶

  • Use AbiPy programmatic interface to implement:

    • Validation of parallel algorithms for np in range(1, N)
    • Stress testing
    • Benchmarks